<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Repositories\Contracts\PolicyUploadInterface;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;
use MakeRep\Services\ParamService;

class PolicyUploadController extends BaseController
{
    public function __construct(PolicyUploadInterface $interface)
    {

        parent::__construct($interface);
    }

    /**
     * 上传携程政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxAdd(Request $request)
    {
        $res = $this->interface->create($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }


    /**
     * 列表
     * @param Request $request
     * @return BaseCollection
     */
    public function ajaxLists(Request $request){

        $pageSize = $request->page_size ? : 10;
        $orderBy = $request->order_by ? : 'desc';

        if ($request->platform && in_array($request->platform,$request->admin_platform)) {

            $where = ParamService::createCondition($request,isset($this->interface->fillable) ? $this->interface->fillable : []);
            $res = $this->interface->pageLists($where,'*',$pageSize,'id',$orderBy);

            if ($res) {

                $policyIds = [];
                foreach ($res as $value) {

                    $policyIds[] = $value['policy_id'];
                }

                $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\SpecialPolicyInterface');
                if ($request->platform == 10){
                    $policyDetail = $specialPolicyInterface->detailSearch($policyIds);


                } elseif($request->platform == 11) {
                    $policyDetail = $specialPolicyInterface->detailMhSearch($policyIds);
                }

                if (is_array($policyDetail) && !empty($policyDetail['TradeResult']) && !empty($policyDetail['TradeResult']['PolicyList'])) {

                    foreach ($res as &$value){

                        foreach ($policyDetail['TradeResult']['PolicyList'] as $val) {

                            if ($val['PolicyID'] == $value['policy_id']) {

                                if ($val['InventoryUnit']['InventoryType'] == 'FIX') {
                                    $value['seat'] = ($value['seat'] - $val['InventoryUnit']['FIX']['SaledTicketNum']).'/'.$value['seat'];
                                } else {

                                    $value['seat'] = '按照ETERM';
                                }
                            }
                        }
                    }
                }
            }


            return new BaseCollection($res);
        }

        return new BaseCollection(new LengthAwarePaginator([],0,10));
    }

    /**
     * 政策编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View|\MakeRep\Resources\BaseResource|\MakeRep\Resources\ErrorResource
     */
    public function update(Request $request)
    {

        //获取政策详情
        $res = $this->interface->policyInfo($request->all());
        if (is_array($res)) {
            return view('policyupload.update',['id'=>$request->id,'policy'=>$res[0]]);
        } else {

            abort(404);
        }

    }

    /**
     * 修改政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxUpdate(Request $request)
    {
        $res = $this->interface->ajaxUpdate($request->all());
        if ($res) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }


    /**
     * 简易更新政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function itemUpdate(Request $request){

        $res = $this->interface->itemUpdate($request->all());
        if ($res) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 删除政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxDelete(Request $request)
    {
        $policy = $this->interface->findById($request->id);;
        if ($policy) {

            $res = $this->interface->deletePolicy($request->platform,$policy->policy_id,'ByID');
            if ($res) {

                return new BaseResource([]);
            }

        }

        return new ErrorResource(['errmsg'=>$res]);
    }
}